package in.muton.edushare.processing.objects;

import in.muton.edushare.components.autocomplete.api.MainInviter;
import in.muton.edushare.wicket.models.CayenneList;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.model.IModel;

import com.edushare.common.vo.Courses;
import com.edushare.common.vo.Curricula;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.UnivDeptLink;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.UniversityCourseLinks;
import com.edushare.common.vo.UserRoles;
import com.edushare.common.vo.Users;

public class UnivDataHandler implements Serializable{

	private static final long serialVersionUID = -989781010431347822L;
	private static final Log log = LogFactory.getLog(UnivDataHandler.class);
	
	public UnivDataHandler(IModel<Universities> model) {
		this.model = model;
	}

	public CayenneList<Departments> getOwnDept() {
		List<Departments> deptList = new ArrayList<Departments>();
		List<UnivDeptLink> ud = model.getObject().getDeptLinks();
		for (UnivDeptLink u : ud) {
			if (u.getAssociationType() == UnivDeptLink.UNIVERSITY_DEPT_OWNER)
				deptList.add(u.getDepartment());
		}
		Collections.sort(deptList);
		return new CayenneList<Departments>(deptList);
	}

	public CayenneList<Departments> getAllDept() {
		List<Departments> cL = new ArrayList<Departments>();
		List<UnivDeptLink> ud = model.getObject().getDeptLinks();
		for (UnivDeptLink u : ud) {
			cL.add(u.getDepartment());
		}
		Collections.sort(cL);
		return new CayenneList<Departments>(cL);
	}

	public CayenneList<Users> getAdmins() {
		return getPeopleByRole(UserRoles.ADMIN_ROLE);
	}
	
	public CayenneList<Users> getEveryOne(){
		CayenneList<Users> u = getAdmins();
		u.add(getFaculty());
		u.add(getStudents());
		return u;
	}

	public CayenneList<Users> getFaculty() {
		return getPeopleByRole(UserRoles.TEACHER_ROLE);
	}
	
	public CayenneList<Users> getPeopleByRole(int role){
		List<Users> cL = new ArrayList<Users>();
		List<UserRoles> ur = model.getObject().getUserRoleList();
		for (UserRoles u : ur) {
			if (u.getRoleType() == role) {
				cL.add(u.getUser());
			}
		}
		Collections.sort(cL);
		CayenneList<Users> l = new CayenneList<Users>(cL);
		return l;	
	}

	public CayenneList<Users> getStudents() {
		return getPeopleByRole(UserRoles.STUDENT_ROLE);
	}
	
	public CayenneList<Courses> getCourses(){
		List<Courses> cL = new ArrayList<Courses>();
		List<UniversityCourseLinks> uC = model.getObject().getLinkedCourses();
		for(UniversityCourseLinks u:uC){
			if(u.getUnivCourseAssnType() == UniversityCourseLinks.UNIV_COURSE_OWNER || 
					u.getUnivCourseAssnType() == UniversityCourseLinks.UNIV_COURSE_SUBSCRIBER)
				cL.add(u.getCourse());
		}
		Collections.sort(cL);
		return new CayenneList<Courses>(cL);
	}
	
	public static CayenneList<Universities> getAllUnivs(){
		CayenneList<Universities> uL = new CayenneList<Universities>(null);
		return uL;
	}
	
	public CayenneList<Curricula> getPrograms(){
		return new CayenneList<Curricula>(model.getObject().getCurricula());
	}
	
	public String getUserRole(Users user){
		List<UserRoles> uR = user.getRoleList();
		for(UserRoles u:uR){
			if(u.getUniversity() == model.getObject()){
				if(u.getRoleType() == UserRoles.ADMIN_ROLE)
					return "University Administrator";
				else if(u.getRoleType() == UserRoles.TEACHER_ROLE)
					return "University Faculty";
				else if(u.getRoleType() == UserRoles.STUDENT_ROLE)
					return "University Student";
			}
		}
		return "Visitor";
	}
	private IModel<Universities> model = null;
}
